<?
/**
 *account Module层，封装了与数据有关的操作
 */
class MAccount
{
	private $_db;
	public function __construct()
	{
		$this->_db = Db::getInstance();
	}
	
	/**
	 *用户名密码验证
	 *@param $userTel 用户手机号
	 *@param $userPwd 用户密码
	 *@return boolean  
	 */
	public function authenticateUser($userTel, $userPwd)
	{
		$sql = "select * from `account` where `tel`='{$userTel}' and `password`='{$userPwd}'";
		$result = $this->_db->num_rows($sql);
		return $result>0 ? true : false;	
	}
	
	/**
	 *@param $userTel 用户手机号
	 *@param $userPwd 用户密码
	 *@return boolean  
	 */
	public function insertUser($userTel, $userPwd)
	{
		$sql = "insert into `account`(`tel`,`password`) values('{$userTel}','{$userPwd}')";
		try
		{
			$this->_db->query($sql);
			return true;
		}
		catch(Exception $e)
		{
			return false;
		}
	}

	/**
	 *@param $userTel 用户手机号
	 *@param $userPwd 用户密码
	 *@return boolean  
	 */	
	public function updateUserPwd($userTel, $userPwd)
	{
		$sql = "update `account` set `password`='{$userPwd}' where `tel`='{$userTel}'";
		try{
			$result = $this->_db->num_rows($sql);
			if($result==1){
				return true;
			}else{
				return false;
			}
		}catch (Exception $e){
			return false;
		}		
	}
	
	/**
	 *查询用户名是否存在(ajax使用)
	 *@param $userTel 用户手机号
	 *@return boolean  
	 */
	public function checkUser($userTel)
	{
		$sql = "select `tel` from `account` where `tel`='{$userTel}'";
		$result = $this->_db->num_rows($sql);
		return $result>0 ? true : false;
	}
	
	/**
	 *更新sessionId
	 *
	 */
	public function setSessionId($userTel,$sessionId)
	{
		$sql = "update `account` set `sessionId`='{$sessionId}' where `tel`={$userTel}";
		$this->_db->query($sql);
	}
	
}
?>